package com.las.nogu.controller.vits;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.las.bot.dao.VitsModelDao;
import com.las.bot.dao.VitsUserDao;
import com.model.VitsModel;
import com.model.VitsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/vits")
public class VitsController {

    @Autowired
    private VitsUserDao vitsUserDao;

    @Autowired
    private VitsModelDao modelDao;


    @RequestMapping("/getAI/{uId}/{token}/{mId}/{rId}/{text}")
    public String auth(@PathVariable("uId") String uId, @PathVariable("token") String token,
                       @PathVariable("mId") String mId, @PathVariable("rId") String rId,
                       @PathVariable("text") String text) throws Exception {
        VitsModel vitsModel = modelDao.findById(mId);
        if (ObjectUtil.isNull(vitsModel)) {
            return "NONE";
        }
        VitsUser vitsUser = vitsUserDao.findByUserAndCode(uId, mId + "-" + rId);
        if (ObjectUtil.isNull(vitsUser)) {
            return "NONE";
        }
        String uuid = vitsUser.getToken();
        if(!uuid.equals(token)){
            return "NONE";
        }
        if(StrUtil.isBlank(text)){
            return "NONE";
        }
        if(text.length() > 30){
            return "NONE";
        }
        String fId = getBase64(text, vitsModel.getModelPath(), vitsModel.getConfigPath(), rId);
        if(StrUtil.isBlank(fId)){
            return "NONE";
        }
        String result = HttpUtil.post("http://vits.natapp1.cc/sovits/getBase64ById?fId=" + fId, (String) null);
        JSONObject object = JSONObject.parseObject(result);
        return object.getString("data");
    }

    private synchronized String getBase64(String text, String m, String c, String r) throws Exception {
        String result = "";
        Socket socket = new Socket("e36ff7293101f071.natapp.cc", 12468);
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        JSONObject object = new JSONObject();
        object.put("model", m);
        object.put("config", c);
        object.put("index", r);
        String encode = Base64.encode(text.getBytes(StandardCharsets.UTF_8));
        object.put("text", encode);
        dos.writeUTF("#" + object.toJSONString());
        DataInputStream dis = new DataInputStream(socket.getInputStream());
        byte[] bs = new byte[10240];
        int len = dis.read(bs);
        String str = new String(bs, 0, len);
        socket.close();
        if (StrUtil.isNotBlank(str)) {
            return str;
        }
        return result;
    }

}
